//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.comprosoft.telnet.command;

import com.comprosoft.telnet.io.IOHandler;
import com.comprosoft.telnet.server.TelnetServer;
import com.comprosoft.utils.ProcessUtils;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

public class KillProcessCommandHandler extends Command {
    private static final Logger logger = Logger.getLogger("KillProcessCommandHandler");
    volatile boolean ifError = false;

    public KillProcessCommandHandler(IOHandler io, TelnetServer telnetServer) {
        super(io, "kill", telnetServer);
    }

    public String getOsCmd() {
        Properties props = System.getProperties();
        String osName = props.getProperty("os.name");
        if (osName.toLowerCase().contains("linux")) {
            return "/bin/sh -c ";
        } else if (osName.toLowerCase().contains("windows")) {
            return "cmd /c ";
        } else {
            throw new RuntimeException("服务器不是linux|windows操作系统");
        }
    }

    public CommandResult handleCommand(String[] args) {
        long startTime = System.currentTimeMillis();
        StringBuffer commandStr = new StringBuffer();
        int i = 0;
        String[] var6 = args;
        int var7 = args.length;

        for(int var8 = 0; var8 < var7; ++var8) {
            String arg = var6[var8];
            if (i != 0) {
                commandStr.append(arg).append(" ");
            }

            ++i;
        }

        logger.log(Level.INFO, "kill process:" + commandStr.toString());

        try {
            long pid = Long.valueOf(commandStr.toString().trim());
            ProcessUtils.killProcessTree(pid);
            CustomProcess customProcess = (CustomProcess)this.getTelnetServer().getLongCustomProcessMap().get(pid);
            if (customProcess != null) {
                customProcess.destory();
                this.getTelnetServer().getLongCustomProcessMap().remove(pid);
            }

            if (this.ifError) {
                return CommandResult.COMMAND_EXIT;
            } else {
                return CommandResult.COMMAND_OK;
            }
        } catch (Exception var10) {
            var10.printStackTrace();
            return CommandResult.COMMAND_EXIT;
        }
    }

    public static void main(String[] args) throws Exception {
        for(int i = 0; i < 20; ++i) {
            String subCommond = null;
            if (i % 2 == 0) {
                subCommond = "start";
            } else {
                subCommond = "stop";
            }

            ProcessBuilder pb = new ProcessBuilder(("cmd /c net " + subCommond + " telcat").split(" "));
            pb.redirectErrorStream(true);
            Process process = null;
            BufferedReader br = null;

            try {
                process = pb.start();
                br = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK"));
                String line = null;

                while((line = br.readLine()) != null) {
                    System.out.println(line);
                }

                process.waitFor();
            } finally {
                if (br != null) {
                    br.close();
                }

                if (process != null) {
                    process.exitValue();
                    process.destroy();
                    Runtime.getRuntime().gc();
                }

            }
        }

    }
}
